import requests
import execjs
import jsonpath
class scrapes:
    def __init__(self):
        self.url = 'https://spa2.scrape.center/api/movie'
        self.headers = {
            'Accept': 'application/json, text/plain, */*',
            'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
            'Cache-Control': 'no-cache',
            'Connection': 'keep-alive',
            'Pragma': 'no-cache',
            'Referer': 'https://spa2.scrape.center/',
            'Sec-Fetch-Dest': 'empty',
            'Sec-Fetch-Mode': 'cors',
            'Sec-Fetch-Site': 'same-origin',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0',
            'sec-ch-ua': '"Microsoft Edge";v="135", "Not-A.Brand";v="8", "Chromium";v="135"',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-platform': '"Windows"',
        }
        self.params = {
            'limit': '10',
            'offset': '0',
            'token': 'MTcxM2NhMDU4MmJlMWQ0NzE1OTNkZDljZTFmZjExMGI2M2M5ZGNmNiwxNzQ1NTcxOTQ4',
        }
        self.js = execjs.compile(open('scrape.js','r',encoding='utf-8').read())
    def request_data(self,page):
        js=self.js.call('main',page)
        print(js['token'])
        self.params['offset']=str(js['offset'])
        self.params['token']=str(js['token'])
     
        response = requests.get('https://spa2.scrape.center/api/movie', params=self.params, headers=self.headers)
        return response.json()
    def jsonpath_data(self,data):
        movies = jsonpath.jsonpath(data,'$..results[*]')
        print(type(movies))
        for movie in movies:
            print(f"电影名称: {movie['name']}, 发布时间: {movie['published_at']}")


if __name__ == '__main__':
    scrape = scrapes()
    data = scrape.request_data(2)
    scrape.jsonpath_data(data)